import openpyxl
from openpyxl.styles import Font
from openpyxl.styles import colors

wb=openpyxl.Workbook()

#Font 设置字体
ws=wb.active
ws.title='Font'

#default 11pt,Calibri 这是excel默认字体格式
#我们想新定义一个字体格式
italic24Font=Font(size=24,italic=True)#italic是设置斜体的意思
ws['B3'].font=italic24Font#将B3单元格字体设置
ws['B3']='24pt Italic'#设置B3单元格内容

#bold是斜体，COLOR_INDEX[2]是红色
boldRedFont=Font(name='Times New Roman',bold=True,color=colors.COLOR_INDEX[2])
ws['A1'].font=boldRedFont
ws['A1']='Bold Red Times New Roman'

#Fomulas 利用公式计算
ws=wb.create_sheet('Formula')
ws['A1']=200
ws['A2']=300
ws['A3']='=SUM(A1:A2)'

#Setting row height and column width 设置行列高度
ws=wb.create_sheet('dimensions')
#设置第一行高度
ws['A1']='TALL row'
ws.row_dimensions[1].height=70#将第一行高度设置为70

#设置B列宽度
ws['B2']='Wide column'
ws.column_dimensions['B'].width=20#将B列宽度设置为20

#Merging cells合并单元格
ws=wb.create_sheet('merged')
ws.merge_cells('A1:D3')
ws['A1']='Twelve cells merged together'
ws.merge_cells('C5:D5')
ws['C5']='Two merged cells'

#Unmerging cells
ws=wb.copy_worksheet(wb['merged'])#拷贝一份表单
ws.title='unmerged'
ws.unmerge_cells('A1:D3')
ws.unmerge_cells('C5:D5')

wb.save('style.xlsx')